#include "add_form.h"
#include "ui_add_form.h"

Add_Form::Add_Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Add_Form)
{
    ui->setupUi(this);
    setWindowModality(Qt::ApplicationModal);
    setWindowTitle("添加/编辑数据");
}

Add_Form::~Add_Form()
{
    delete ui;
}

void Add_Form::showWidget(QVariant data)
{
    parseData();
    init();
    if(data.isNull()){
    }
    else{
        //编辑
        QString pm = data.toMap().value("pm").toString();
        QString tip = data.toMap().value("tip").toString();
        QVariantList msgs = data.toMap().value("msgs").toList();
        QVariantList items = data.toMap().value("items").toList();

        ui->comboBox->setCurrentText(pm);
        ui->lineEdit_5->setText(tip);

        QVariantMap msg = msgs.at(0).toMap();
        QVariantMap item = items.at(0).toMap();

        QVariantList structList = msg.value("structList").toList();
        QString structId = msg.value("structId").toString();
        QString structName = msg.value("structName").toString();

        ui->lineEdit->setText(structId);

        ui->lineEdit_4->setText("");
        ui->comboBox_2->setCurrentText(structName);

        QString plainText = "";
        int len = structList.length();
        for(int i=0; i<len; i++){
            QVariantMap m = structList.at(i).toMap();
            plainText += m.value("name").toString()+":"+m.value("bits").toString()+":"+m.value("value").toString()+"\n";
        }
        ui->plainTextEdit->setPlainText(plainText);

        QVariantList propertyList = item.value("propertyList").toList();
        QString itemName = item.value("itemName").toString();

        ui->lineEdit_3->setText(itemName);
        len = propertyList.length();
        for(int i=0; i<len; i++){
            QVariantMap m = propertyList.at(i).toMap();
            ui->tableWidget_2->setItem(i, 0, new QTableWidgetItem(m.value("property").toString()));
            ui->tableWidget_2->setItem(i, 1, new QTableWidgetItem(m.value("value").toString()));
        }
    }
    this->show();
}

//设置解析数据
void Add_Form::parseData()
{
    _stuctNames = _name_to_id.keys();
}

void Add_Form::on_pushButton_clicked()
{
    //save
    if(ui->lineEdit->text().isEmpty()){
        QMessageBox msgBox;
        msgBox.setText("结构体id 数据不能空！");
        msgBox.exec();
        return;
    }

//    if(ui->lineEdit_4->text().isEmpty()){
//        QMessageBox msgBox;
//        msgBox.setText("结构体名字 数据不能空！");
//        msgBox.exec();
//        return;
//    }

    if(ui->lineEdit_5->text().isEmpty()){
        QMessageBox msgBox;
        msgBox.setText("tip 数据不能空！");
        msgBox.exec();
        return;
    }

    if(ui->lineEdit_3->text().isEmpty()){
        QMessageBox msgBox;
        msgBox.setText("item名字 数据不能空！");
        msgBox.exec();
        return;
    }

    if(ui->plainTextEdit->toPlainText().isEmpty()){
        QMessageBox msgBox;
        msgBox.setText("结构体列表 数据不能空！");
        msgBox.exec();
        return;
    }

    if(ui->tableWidget_2->item(0,0) == nullptr
            || ui->tableWidget_2->item(0,1) == nullptr
            || ui->tableWidget_2->item(0,0)->text().isEmpty()
            || ui->tableWidget_2->item(0,1)->text().isEmpty()){
        QMessageBox msgBox;
        msgBox.setText("属性列表 数据不能空！");
        msgBox.exec();
        return;
    }

    QVariantMap map;

    QVariantList msgs;
    QVariantMap msg1;
    msg1.insert("structId", ui->lineEdit->text());
    msg1.insert("structName", ui->comboBox_2->currentText());

    QVariantList msg1List;
    QStringList msgl = ui->plainTextEdit->toPlainText().split("\n");

    foreach (QString str, msgl) {
        if(str.isEmpty()) continue;
        QStringList s = str.split(":");
        QVariantMap msgMap;
        msgMap.insert("name", s.at(0));
        msgMap.insert("bits", s.at(1));
        msgMap.insert("value", s.at(2));
        msg1List.append(msgMap);
    }

    msg1.insert("structList", msg1List);
    msgs.append(msg1);
    //------------------------可以添加第二个msg


    QVariantList msgItems;
    QVariantMap msgItem1;
    msgItem1.insert("itemName", ui->lineEdit_3->text());
    QVariantList msgItem1List;
    for(int i=0; i<ui->tableWidget_2->rowCount(); i++){
        QTableWidgetItem *tableItem = ui->tableWidget_2->item(i, 0);
        if(tableItem == nullptr) break;
        if(tableItem->text().isEmpty()) break;
        QTableWidgetItem *tableItem2 = ui->tableWidget_2->item(i, 1);

        QVariantMap msgMap;
        msgMap.insert("property", tableItem->text());
        msgMap.insert("value", tableItem2->text());
        msgItem1List.append(msgMap);
    }
    msgItem1.insert("propertyList", msgItem1List);

    msgItems.append(msgItem1);
    //------------------------可以添加第二个Item


    //end
    map.insert("pm", ui->comboBox->currentText());
    map.insert("msgs", msgs);
    map.insert("items", msgItems);
    if(ui->lineEdit_5->text().isEmpty())
        map.insert("tip", "tip");
    else
        map.insert("tip", ui->lineEdit_5->text());

    emit addData(QVariant(map));
    this->close();
}

void Add_Form::init()
{
    ui->lineEdit->setEnabled(false);

    QStringList items;
    items<<"Animation"<<"D1"<<"D2";
    ui->comboBox->addItems(items);

    ui->tableWidget_2->setColumnCount(2);
    ui->tableWidget_2->clear();

    QStringList heads2;
    heads2<<"属性"<<"值";
    ui->tableWidget_2->setHorizontalHeaderLabels(heads2);
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget_2->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget_2->setRowCount(3);

    ui->comboBox->setCurrentIndex(0);
    ui->lineEdit->setText("");
    ui->lineEdit_3->setText("");
    ui->lineEdit_4->setText("");
    ui->lineEdit_5->setText("");
    ui->comboBox_2->clear();

    ui->comboBox_2->addItems(_stuctNames);
    ui->comboBox_2->setCurrentIndex(0);
}

void Add_Form::on_lineEdit_4_textChanged(const QString &arg1)
{
    //检索
    if(arg1.isEmpty()){
        ui->comboBox_2->clear();
        ui->comboBox_2->addItems(_stuctNames);
        ui->comboBox_2->setCurrentIndex(0);
    }
    else{
        ui->comboBox_2->clear();

        foreach (QString _stuct, _stuctNames) {
            if(_stuct.contains(arg1.toUpper()) || _stuct.contains(arg1.toLower())){
                ui->comboBox_2->addItem(_stuct);
            }
        }

        if(ui->comboBox_2->count()>0)
            ui->comboBox_2->setCurrentIndex(0);
    }
}

void Add_Form::on_comboBox_2_currentIndexChanged(int index)
{
    //
    if(index == -1) {
        ui->plainTextEdit->setPlainText("");
        return;
    }

    QString structName = ui->comboBox_2->currentText();
    int id = _name_to_id.value(structName);
    ui->lineEdit->setText(QString::number(id));

    int len = _parseHeadMsgList.length();
    for(int i=0; i<len; i++){
        QVariantMap map = _parseHeadMsgList.at(i).toMap();
        if(map.value("name").toString() == structName){

            QVariantList msgList = map.value("msgList").toList();

            QString plainText = "";
            int len = msgList.length();
            for(int i=0; i<len; i++){
                QVariantMap m = msgList.at(i).toMap();

                plainText += m.value("name").toString()+":"+m.value("bits").toString()+":"+m.value("value").toString()+"\n";
            }
            ui->plainTextEdit->setPlainText(plainText);
            break;
        }
    }

}
